/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.table.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.util.*;
import javax.swing.border.TitledBorder;

/**
 * <p>The main frame in the standalone version of the application</p>
 * @author Andriy Nikolov
 * @version 1.0
 */

public class MainFrame extends JFrame implements SociogramProducer, TaskPersonDiagramProducer {
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  JMenuBar jMenuBar = new JMenuBar();
  JMenu jMenuFile = new JMenu();
  JMenuItem jMenuItemFileOpen = new JMenuItem();
  JMenuItem jMenuItemFileSaveAs = new JMenuItem();
  JMenu jMenuSociogram = new JMenu();
  JMenu jMenuProcessLog = new JMenu();
  JPanel jPanelToolbar = new JPanel();
  JButton jButtonOpen = new JButton();
  ImageIcon iconOpen = new ImageIcon("icons/openwf.gif");
  ImageIcon iconSave = new ImageIcon("icons/save.gif");
  ImageIcon iconSociogram = new ImageIcon("icons/handshak.gif");
  ImageIcon iconExit = new ImageIcon("icons/exit.gif");
  ImageIcon iconGraph = new ImageIcon("icons/ltrans.gif");
  ImageIcon iconGraph2 = new ImageIcon("icons/dtrans.gif");
  ImageIcon iconProcess = new ImageIcon("icons/workflow.gif");
  ImageIcon iconPlotChart = new ImageIcon("icons/plot32.gif");
  ImageIcon iconFilter = new ImageIcon("icons/filter.gif");
  ImageIcon iconSetup = new ImageIcon("icons/preferences24.gif");
  JButton jButtonSave = new JButton();
  JButton jButtonSociogram = new JButton();
  BoxLayout2 boxLayout2 = new BoxLayout2();
  JMenuItem jMenuItemExit = new JMenuItem();
  JButton jButtonExit = new JButton();
  JPanel sociogramMatrixPanel = new JPanel();
  JScrollPane jScrollPaneSociogramInner = new JScrollPane();
  final JFileChooser fileChooser = new JFileChooser();

  EMailLog eMailLog;
  Sociogram sociogram = null;
  SociogramFrame sociogramFrame;
  TaskPersonTableFrame taskPersonTableFrame;
  CasePersonTableFrame casePersonTableFrame;
  ProcessLog processLog;
  SociogramOptionsDialog sociogramOptionsDialog;
  TaskPersonTableOptionsDialog taskPersonTableOptionsDialog;
  boolean useThreadsOnly = false;
  boolean showOnlyMaximalArcs = false;
  boolean showOnlyOriginators = false;
  JTabbedPane jTabbedPane1 = new JTabbedPane();
  JScrollPane jScrollPaneEMailLog = new JScrollPane();
  JPanel jPanelEMailLog = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JTextArea jTextAreaLog = new JTextArea();
  JScrollPane jScrollPaneSociogram = new JScrollPane();
  JPanel jPanelSociogram = new JPanel();
  JTable jTableSociogramMatrix = new JTable();
  BorderLayout borderLayout3 = new BorderLayout();
  SociogramTableModel sociogramTableModel;
  JButton jButtonVisualize = new JButton();
  JScrollPane jScrollPaneProcessLog = new JScrollPane();
  JPanel jPanelProcessLog = new JPanel();
  BorderLayout borderLayout4 = new BorderLayout();
  JTextArea jTextAreaProcessLog = new JTextArea();
  JButton jButtonProcessLog = new JButton();
  Component strut1;
  Component strut2;
  Component strut3;
  JButton jButtonSaveSociogram = new JButton();
  JButton jButtonSaveProcessLog = new JButton();
  JMenuItem jMenuItemSociogramBuild = new JMenuItem();
  JMenuItem jMenuItemSociogramVisualize = new JMenuItem();
  JMenuItem jMenuItemSociogramSave = new JMenuItem();
  JMenuItem jMenuItemProcessLogBuild = new JMenuItem();
  JMenuItem jMenuItemProcessLogSave = new JMenuItem();
  JButton jButtonChart = new JButton();
  Component strut4;
  FrequencyChart frequencyChart;
  FrequencyChartFrame frequencyChartFrame;
  JButton jButtonFilterLog = new JButton();
  EMailLogFrame eMailLogFrame;
  JMenu jMenuChart = new JMenu();
  JMenuItem jMenuItemBuildChart = new JMenuItem();
  JMenuItem jMenuItemFilterLog = new JMenuItem();
  JButton jButtonProcessLogSetup = new JButton();
  ProcessLogSetupFrame processLogSetupFrame = null;
  JMenuItem jMenuItemProcessLogOptions = new JMenuItem();
  JPanel jPanelStatus = new JPanel();
  TitledBorder titledBorder1 = new TitledBorder("");
  JButton jButtonTaskPersonTable = new JButton();
  JButton jButtonCasePersonTable = new JButton();
  JMenuItem jMenuItemProcessLogTaskPersonDiagram = new JMenuItem();
  JMenuItem jMenuItemProcessLogCasePersonDiagram = new JMenuItem();

  //Construct the frame
  public MainFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    strut1 = Box.createHorizontalStrut(8);
    strut2 = Box.createHorizontalStrut(8);
    strut3 = Box.createHorizontalStrut(8);
    strut4 = Box.createHorizontalStrut(8);
    contentPane.setLayout(borderLayout1);
    this.setJMenuBar(jMenuBar);
    this.setSize(new Dimension(400, 300));
    this.setTitle("E-mail Log Analyzer");
    jMenuFile.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuFile.setText("File");
    jMenuItemFileOpen.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemFileOpen.setText("Open");
    jMenuItemFileOpen.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemFileOpen_actionPerformed(e);
      }
    });
    jMenuItemFileSaveAs.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemFileSaveAs.setText("Save  as...");
    jMenuItemFileSaveAs.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemFileSaveAs_actionPerformed(e);
      }
    });
    jMenuItemFileSaveAs.setEnabled(false);
    jMenuSociogram.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuSociogram.setText("Sociogram");
    jMenuProcessLog.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuProcessLog.setActionCommand("Process log");
    jMenuProcessLog.setText("Process log");
    jButtonOpen.setMaximumSize(new Dimension(27, 27));
    jButtonOpen.setMinimumSize(new Dimension(27, 27));
    jButtonOpen.setPreferredSize(new Dimension(27, 27));
    jButtonOpen.setToolTipText("Add e-mail log");
    jButtonOpen.setIcon(iconOpen);
    jButtonOpen.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonOpen_actionPerformed(e);
      }
    });
    jPanelToolbar.setBorder(BorderFactory.createEtchedBorder());
    jPanelToolbar.setLayout(boxLayout2);
    jButtonSave.setMaximumSize(new Dimension(27, 27));
    jButtonSave.setMinimumSize(new Dimension(27, 27));
    jButtonSave.setPreferredSize(new Dimension(27, 27));
    jButtonSave.setToolTipText("Save e-mail log");
    jButtonSave.setIcon(iconSave);
    jButtonSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonSave_actionPerformed(e);
      }
    });
    jButtonSave.setEnabled(false);
    jButtonSociogram.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonSociogram_actionPerformed(e);
      }
    });
    jButtonSociogram.setIcon(iconSociogram);
    jButtonSociogram.setPreferredSize(new Dimension(27, 27));
    jButtonSociogram.setToolTipText("Calculate a sociogram");
    jButtonSociogram.setMinimumSize(new Dimension(27, 27));
    jButtonSociogram.setMaximumSize(new Dimension(27, 27));
    jButtonSociogram.setEnabled(false);
    jMenuItemExit.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemExit.setText("Exit");
    jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemExit_actionPerformed(e);
      }
    });
    jButtonExit.setMaximumSize(new Dimension(27, 27));
    jButtonExit.setMinimumSize(new Dimension(27, 27));
    jButtonExit.setPreferredSize(new Dimension(27, 27));
    jButtonExit.setIcon(iconExit);
    jButtonExit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonExit_actionPerformed(e);
      }
    });
    jPanelEMailLog.setLayout(borderLayout2);
    jPanelSociogram.setLayout(borderLayout3);
    jButtonVisualize.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonVisualize_actionPerformed(e);
      }
    });
    jButtonVisualize.setIcon(iconGraph);
    jButtonVisualize.setPreferredSize(new Dimension(27, 27));
    jButtonVisualize.setToolTipText("Show created sociogram");
    jButtonVisualize.setMinimumSize(new Dimension(27, 27));
    jButtonVisualize.setMaximumSize(new Dimension(27, 27));
    jButtonVisualize.setEnabled(false);
    jPanelProcessLog.setLayout(borderLayout4);
    jButtonProcessLog.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonProcessLog_actionPerformed(e);
      }
    });
    jButtonProcessLog.setIcon(iconProcess);
    jButtonProcessLog.setPreferredSize(new Dimension(27, 27));
    jButtonProcessLog.setToolTipText("Build a process log");
    jButtonProcessLog.setMinimumSize(new Dimension(27, 27));
    jButtonProcessLog.setMaximumSize(new Dimension(27, 27));
    jButtonProcessLog.setEnabled(false);
    jButtonSaveSociogram.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonSaveSociogram_actionPerformed(e);
      }
    });
    jButtonSaveSociogram.setIcon(iconSave);
    jButtonSaveSociogram.setToolTipText("Save sociogram");
    jButtonSaveSociogram.setPreferredSize(new Dimension(27, 27));
    jButtonSaveSociogram.setMinimumSize(new Dimension(27, 27));
    jButtonSaveSociogram.setMaximumSize(new Dimension(27, 27));
    jButtonSaveSociogram.setEnabled(false);
    jButtonSaveProcessLog.setMaximumSize(new Dimension(27, 27));
    jButtonSaveProcessLog.setMinimumSize(new Dimension(27, 27));
    jButtonSaveProcessLog.setPreferredSize(new Dimension(27, 27));
    jButtonSaveProcessLog.setToolTipText("Save process log");
    jButtonSaveProcessLog.setIcon(iconSave);
    jButtonSaveProcessLog.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonSaveProcessLog_actionPerformed(e);
      }
    });
    jButtonSaveProcessLog.setEnabled(false);
    jMenuItemSociogramBuild.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemSociogramBuild.setText("Build");
    jMenuItemSociogramBuild.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemSociogramBuild_actionPerformed(e);
      }
    });
    jMenuItemSociogramBuild.setEnabled(false);
    jMenuItemSociogramVisualize.setFont(new java.awt.Font("Tahoma", Font.BOLD,
        11));
    jMenuItemSociogramVisualize.setText("Visualize");
    jMenuItemSociogramVisualize.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemSociogramVisualize_actionPerformed(e);
      }
    });
    jMenuItemSociogramVisualize.setEnabled(false);
    jMenuItemSociogramSave.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemSociogramSave.setText("Save as...");
    jMenuItemSociogramSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemSociogramSave_actionPerformed(e);
      }
    });
    jMenuItemSociogramSave.setEnabled(false);
    jMenuItemProcessLogBuild.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemProcessLogBuild.setText("Build");
    jMenuItemProcessLogBuild.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemProcessLogBuild_actionPerformed(e);
      }
    });
    jMenuItemProcessLogBuild.setEnabled(false);
    jMenuItemProcessLogSave.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemProcessLogSave.setText("Save as...");
    jMenuItemProcessLogSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemProcessLogSave_actionPerformed(e);
      }
    });
    jMenuItemProcessLogSave.setEnabled(false);
    jButtonChart.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonChart_actionPerformed(e);
      }
    });
    jButtonChart.setIcon(iconPlotChart);
    jButtonChart.setToolTipText("View message frequency chart");
    jButtonChart.setPreferredSize(new Dimension(27, 27));
    jButtonChart.setMinimumSize(new Dimension(27, 27));
    jButtonChart.setMaximumSize(new Dimension(27, 27));
    jButtonChart.setEnabled(false);
    jButtonFilterLog.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonFilterLog_actionPerformed(e);
      }
    });
    jButtonFilterLog.setIcon(iconFilter);
    jButtonFilterLog.setToolTipText("Select messages for analysis");
    jButtonFilterLog.setPreferredSize(new Dimension(27, 27));
    jButtonFilterLog.setMinimumSize(new Dimension(27, 27));
    jButtonFilterLog.setMaximumSize(new Dimension(27, 27));
    jButtonFilterLog.setEnabled(false);
    jMenuChart.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuChart.setText("Chart");
    jMenuItemBuildChart.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemBuildChart.setText("Build chart...");
    jMenuItemBuildChart.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemBuildChart_actionPerformed(e);
      }
    });
    jMenuItemBuildChart.setEnabled(false);
    jMenuItemFilterLog.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemFilterLog.setText("Filter log");
    jMenuItemFilterLog.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemFilterLog_actionPerformed(e);
      }
    });
    jMenuItemFilterLog.setEnabled(false);
    jButtonProcessLogSetup.setMaximumSize(new Dimension(27, 27));
    jButtonProcessLogSetup.setMinimumSize(new Dimension(27, 27));
    jButtonProcessLogSetup.setPreferredSize(new Dimension(27, 27));
    jButtonProcessLogSetup.setToolTipText("Process log options");
    jButtonProcessLogSetup.setIcon(iconSetup);
    jButtonProcessLogSetup.setText("");
    jButtonProcessLogSetup.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonProcessLogSetup_actionPerformed(e);
      }
    });
    jButtonProcessLogSetup.setEnabled(false);
    jMenuItemProcessLogOptions.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemProcessLogOptions_actionPerformed(e);
      }
    });
    jMenuItemProcessLogOptions.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemProcessLogOptions.setText("Options...");
    jMenuItemProcessLogOptions.setEnabled(false);
    jPanelStatus.setBorder(titledBorder1);
    jPanelStatus.setToolTipText("");
    jTextAreaLog.setBorder(null);
    jTabbedPane1.setFont(new java.awt.Font("Dialog", Font.BOLD, 11));
    jMenuBar.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jButtonTaskPersonTable.setMaximumSize(new Dimension(27, 27));
    jButtonTaskPersonTable.setMinimumSize(new Dimension(27, 27));
    jButtonTaskPersonTable.setPreferredSize(new Dimension(27, 27));
    jButtonTaskPersonTable.setToolTipText("Show the task-person diagram");
    jButtonTaskPersonTable.setActionCommand("");
    jButtonTaskPersonTable.setIcon(iconGraph);
    jButtonTaskPersonTable.setText("");
    jButtonTaskPersonTable.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonTaskPersonTable_actionPerformed(e);
      }
    });
    jButtonTaskPersonTable.setEnabled(false);
    jButtonCasePersonTable.setMaximumSize(new Dimension(27, 27));
    jButtonCasePersonTable.setMinimumSize(new Dimension(27, 27));
    jButtonCasePersonTable.setPreferredSize(new Dimension(27, 27));
    jButtonCasePersonTable.setToolTipText("Show the case-person diagram");
    jButtonCasePersonTable.setIcon(iconGraph2);
    jButtonCasePersonTable.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonCasePersonTable_actionPerformed(e);
      }
    });
    jButtonCasePersonTable.setEnabled(false);
    jMenuItemProcessLogTaskPersonDiagram.setFont(new java.awt.Font("Tahoma",
        Font.BOLD, 11));
    jMenuItemProcessLogTaskPersonDiagram.setText("Task-person diagram");
    jMenuItemProcessLogTaskPersonDiagram.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemProcessLogTaskPersonDiagram_actionPerformed(e);
      }
    });
    jMenuItemProcessLogTaskPersonDiagram.setEnabled(false);
    jMenuItemProcessLogCasePersonDiagram.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
    jMenuItemProcessLogCasePersonDiagram.setText("Case-person diagram");
    jMenuItemProcessLogCasePersonDiagram.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemProcessLogCasePersonDiagram_actionPerformed(e);
      }
    });
    jMenuItemProcessLogCasePersonDiagram.setEnabled(false);
    jMenuBar.add(jMenuFile);
    jMenuBar.add(jMenuChart);
    jMenuBar.add(jMenuSociogram);
    jMenuBar.add(jMenuProcessLog);
    jMenuFile.add(jMenuItemFileOpen);
    jMenuFile.add(jMenuItemFilterLog);
    jMenuFile.add(jMenuItemFileSaveAs);
    jMenuFile.add(jMenuItemExit);
    jPanelToolbar.add(jButtonOpen, null);
    jPanelToolbar.add(jButtonSave, null);
    jPanelToolbar.add(jButtonFilterLog, null);
    jPanelToolbar.add(strut1, null);
    jPanelToolbar.add(jButtonChart, null);
    jPanelToolbar.add(strut4, null);
    jPanelToolbar.add(jButtonSociogram, null);
    jPanelToolbar.add(jButtonVisualize, null);
    jPanelToolbar.add(jButtonSaveSociogram, null);
    jPanelToolbar.add(strut2, null);
    jPanelToolbar.add(jButtonProcessLogSetup);
    jPanelToolbar.add(jButtonProcessLog, null);
    jPanelToolbar.add(jButtonSaveProcessLog, null);
    jPanelToolbar.add(jButtonTaskPersonTable);
    jPanelToolbar.add(jButtonCasePersonTable);
    jPanelToolbar.add(strut3, null);
    jPanelToolbar.add(jButtonExit, null);
    contentPane.add(jTabbedPane1, BorderLayout.CENTER);
    jTabbedPane1.add(jScrollPaneEMailLog, "E-mail log");
    jScrollPaneEMailLog.getViewport().add(jPanelEMailLog, null);
    jPanelEMailLog.add(jTextAreaLog, BorderLayout.CENTER);
    jScrollPaneProcessLog.getViewport().add(jPanelProcessLog, null);
    jPanelProcessLog.add(jTextAreaProcessLog, BorderLayout.CENTER);
    jMenuSociogram.add(jMenuItemSociogramBuild);
    jMenuSociogram.add(jMenuItemSociogramVisualize);
    jMenuSociogram.add(jMenuItemSociogramSave);
    jMenuProcessLog.add(jMenuItemProcessLogBuild);
    jMenuProcessLog.add(jMenuItemProcessLogSave);
    jMenuProcessLog.add(jMenuItemProcessLogOptions);
    jMenuProcessLog.add(jMenuItemProcessLogTaskPersonDiagram);
    jMenuProcessLog.add(jMenuItemProcessLogCasePersonDiagram);
    jMenuChart.add(jMenuItemBuildChart); //    jTabbedPane1.add(jScrollPaneSociogram,  "Sociogram");
    contentPane.add(jPanelToolbar, java.awt.BorderLayout.NORTH); //    contentPane.add(jPanelStatus, java.awt.BorderLayout.SOUTH);
  }

  public void setUseThreadsOnly(boolean value) {
    useThreadsOnly = value;
  }

  public void setShowOnlyMaximalArcs(boolean value) {
    showOnlyMaximalArcs = value;
  }

  public void setShowOnlyOriginators(boolean value) {
    showOnlyOriginators = value;
  }

  public void setEMailLog(EMailLog log) {
    eMailLog = log;
  }

  public void setProcessLog(ProcessLog log) {
    processLog = log;
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void jButtonSociogram_actionPerformed(ActionEvent e) {
    Dimension minimumSize;
    sociogramOptionsDialog = new SociogramOptionsDialog(this, this, "Sociogram options", true);
    sociogramOptionsDialog.setLocationRelativeTo(this);
    sociogramOptionsDialog.validate();
    sociogramOptionsDialog.setVisible(true);

    if(!sociogramOptionsDialog.isOK())
      return;
    sociogram = eMailLog.getNewSociogram(useThreadsOnly);
    if(sociogram!=null) {
      sociogramTableModel = new SociogramTableModel(sociogram);
      jTableSociogramMatrix.setModel(sociogramTableModel);
      jTableSociogramMatrix.getColumnModel().getColumn(0).setMinWidth(80);
      for (int i=1;i<jTableSociogramMatrix.getModel().getColumnCount();i++) {
        jTableSociogramMatrix.getColumnModel().getColumn(i).setMinWidth(50);
      }
      minimumSize = jTableSociogramMatrix.getMinimumSize();
      minimumSize.width = jTableSociogramMatrix.getModel().getColumnCount()*50+30;
      jTableSociogramMatrix.setMinimumSize(minimumSize);
      jTableSociogramMatrix.setPreferredSize(minimumSize);
      jTableSociogramMatrix.setPreferredScrollableViewportSize(minimumSize);

      if(jTabbedPane1.indexOfTab("Sociogram")==-1) {
        jScrollPaneSociogramInner.setSize(minimumSize);
        jScrollPaneSociogramInner.getViewport().add(jTableSociogramMatrix);
//        jScrollPaneSociogram = new JScrollPane();
//        jScrollPaneSociogram.getViewport().setMinimumSize(minimumSize);
        sociogramMatrixPanel.setSize(minimumSize);
        sociogramMatrixPanel.setPreferredSize(minimumSize);
        sociogramMatrixPanel.add(jScrollPaneSociogramInner);
        jScrollPaneSociogram.getViewport().add(sociogramMatrixPanel);
        jTabbedPane1.insertTab("Sociogram", null, jPanelSociogram,  "Sociogram matrix", 1);
//        jScrollPaneSociogram.getViewport().add(jPanelSociogram, null);
        jPanelSociogram.add(jScrollPaneSociogram,  BorderLayout.CENTER);
      } else {
        jTableSociogramMatrix.revalidate();
      }

      jTabbedPane1.setSelectedComponent(jPanelSociogram);
      jButtonSaveSociogram.setEnabled(true);
      jButtonVisualize.setEnabled(true);
      jMenuItemSociogramSave.setEnabled(true);
      jMenuItemSociogramVisualize.setEnabled(true);
    }
  }

  void jButtonExit_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  void jButtonOpen_actionPerformed(ActionEvent e) {
    File file;
    int messagesLoaded;

    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
    fileChooser.setFileFilter(new XMLFileFilter());
    int fileChooserValue = fileChooser.showOpenDialog(this);
    if(fileChooserValue==JFileChooser.APPROVE_OPTION) {
      file = fileChooser.getSelectedFile();
      jTextAreaLog.append("Loading e-mail log from file: "+file.getName()+"\n");
      messagesLoaded = eMailLog.loadFromFile(file.getAbsolutePath());
      if(messagesLoaded!=-1) {
        jTextAreaLog.append("Loading complete:\n");
        jTextAreaLog.append(messagesLoaded+" new messages added \n");
        jTextAreaLog.append("Total messages: "+eMailLog.getMessageList().size()+"\n");
        jButtonChart.setEnabled(true);
        jButtonFilterLog.setEnabled(true);
        jButtonSave.setEnabled(true);
        jButtonProcessLog.setEnabled(true);
        jButtonProcessLogSetup.setEnabled(true);
        jButtonSociogram.setEnabled(true);
        jMenuItemBuildChart.setEnabled(true);
        jMenuItemFileSaveAs.setEnabled(true);
        jMenuItemFilterLog.setEnabled(true);
        jMenuItemSociogramBuild.setEnabled(true);
        jMenuItemProcessLogBuild.setEnabled(true);
        jMenuItemProcessLogOptions.setEnabled(true);
      } else {
        jTextAreaLog.append("Error: could not read file\n");
      }
    }
  }

  void jButtonSave_actionPerformed(ActionEvent e) {
    File file;
    PrintWriter writer;
    int fileChooserValue;

    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
    fileChooserValue = fileChooser.showSaveDialog(this);
    if(fileChooserValue==JFileChooser.APPROVE_OPTION) {
      file = fileChooser.getSelectedFile();
      jTextAreaLog.append("Saving e-mail log to file: "+file.getName()+"\n");
      try {
        writer = new PrintWriter(new FileWriter(file));
        eMailLog.save(writer);
        writer.close();
      } catch (IOException exc) {
        exc.printStackTrace();
      }
    }
  }

  void jButtonVisualize_actionPerformed(ActionEvent e) {
    if(sociogram==null) {
      JOptionPane.showMessageDialog(this, "Create a sociogram first");
      return;
    }
    sociogramFrame = new SociogramFrame();
    sociogramFrame.setSociogram(sociogram);
    sociogramFrame.validate();

    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = sociogramFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    sociogramFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    sociogramFrame.setVisible(true);
    sociogramFrame.displaySociogram();
  }

  void jButtonProcessLog_actionPerformed(ActionEvent e) {
    File file;
    PrintWriter writer;
    int fileChooserValue;

    if(jTabbedPane1.indexOfTab("Process Log")==-1) {
      jTabbedPane1.add("Process Log", jScrollPaneProcessLog);
    }
    jTabbedPane1.setSelectedComponent(jScrollPaneProcessLog);
    processLog.build();
//    processLog = eMailLog.getProcessLog();
    processLog.write(jTextAreaProcessLog);
    jButtonSaveProcessLog.setEnabled(true);
    jButtonTaskPersonTable.setEnabled(true);
    jButtonCasePersonTable.setEnabled(true);
    jMenuItemProcessLogSave.setEnabled(true);
    jMenuItemProcessLogTaskPersonDiagram.setEnabled(true);
    jMenuItemProcessLogCasePersonDiagram.setEnabled(true);
  }

  void jButtonSaveSociogram_actionPerformed(ActionEvent e) {
    File file;
    PrintWriter writer;
    int fileChooserValue;
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
    fileChooserValue = fileChooser.showSaveDialog(this);
    if(fileChooserValue==JFileChooser.APPROVE_OPTION) {
      file = fileChooser.getSelectedFile();
      jTextAreaLog.append("Saving sociogram to file: "+file.getName()+"\n");
      try {
        writer = new PrintWriter(new FileWriter(file));
        sociogram.save(writer);
        writer.close();
      } catch (IOException exc) {
        exc.printStackTrace();
      }
    }
  }

  void jButtonSaveProcessLog_actionPerformed(ActionEvent e) {
    File file;
    PrintWriter writer;
    int fileChooserValue;

    if(processLog == null) {
      JOptionPane.showMessageDialog(this, "Create a process log first");
      return;
    }

    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
    fileChooserValue = fileChooser.showSaveDialog(this);
    if(fileChooserValue==JFileChooser.APPROVE_OPTION) {
      file = fileChooser.getSelectedFile();
      jTextAreaProcessLog.append("Saving process log to file: "+file.getName()+"\n");
      try {
        writer = new PrintWriter(new FileWriter(file));
        processLog.save(writer);
        writer.close();
      } catch (IOException exc) {
        exc.printStackTrace();
      }
    }
  }

  void jMenuItemFileOpen_actionPerformed(ActionEvent e) {
    jButtonOpen_actionPerformed(e);
  }

  void jMenuItemFileSaveAs_actionPerformed(ActionEvent e) {
    jButtonSave_actionPerformed(e);
  }

  void jMenuItemExit_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  void jMenuItemSociogramBuild_actionPerformed(ActionEvent e) {
    jButtonSociogram_actionPerformed(e);
  }

  void jMenuItemSociogramVisualize_actionPerformed(ActionEvent e) {
    jButtonVisualize_actionPerformed(e);
  }

  void jMenuItemSociogramSave_actionPerformed(ActionEvent e) {
    jButtonSaveSociogram_actionPerformed(e);
  }

  void jMenuItemProcessLogBuild_actionPerformed(ActionEvent e) {
    jButtonProcessLog_actionPerformed(e);
  }

  void jMenuItemProcessLogSave_actionPerformed(ActionEvent e) {
    jButtonSaveProcessLog_actionPerformed(e);
  }

  void jButtonChart_actionPerformed(ActionEvent e) {
    frequencyChart = new FrequencyChart(eMailLog);
    frequencyChartFrame = new FrequencyChartFrame(frequencyChart);
    frequencyChartFrame.validate();
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frequencyChartFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frequencyChartFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frequencyChartFrame.setVisible(true);
  }

  void jButtonFilterLog_actionPerformed(ActionEvent e) {
    eMailLogFrame = new EMailLogFrame(eMailLog);
    eMailLogFrame.validate();
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = eMailLogFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    eMailLogFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    eMailLogFrame.setVisible(true);
  }

  public void jMenuItemBuildChart_actionPerformed(ActionEvent e) {
    jButtonChart_actionPerformed(e);
  }

  public void jMenuItemFilterLog_actionPerformed(ActionEvent e) {
    jButtonFilterLog_actionPerformed(e);
  }

  public void jButtonProcessLogSetup_actionPerformed(ActionEvent e) {
    if (processLogSetupFrame==null) {
      processLogSetupFrame = new ProcessLogSetupFrame(eMailLog, processLog);
    }
    processLogSetupFrame.validate();
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = processLogSetupFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    processLogSetupFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    processLogSetupFrame.setVisible(true);
  }

  public void jMenuItemProcessLogOptions_actionPerformed(ActionEvent e) {
    jButtonProcessLogSetup_actionPerformed(e);
  }

  public void jButtonTaskPersonTable_actionPerformed(ActionEvent e) {
    TaskPersonTable theTable;
    if(processLog==null) {
      JOptionPane.showMessageDialog(this, "Create a process log first");
      return;
    }
    taskPersonTableOptionsDialog = new TaskPersonTableOptionsDialog(this, this, "Task-person diagram options", true);
    taskPersonTableOptionsDialog.setLocationRelativeTo(this);
    taskPersonTableOptionsDialog.validate();
    taskPersonTableOptionsDialog.setVisible(true);

    if (!taskPersonTableOptionsDialog.isOK())
      return;

    taskPersonTableFrame = new TaskPersonTableFrame();
    taskPersonTableFrame.setShowOnlyMaximalArcs(showOnlyMaximalArcs);
    theTable = processLog.getTaskPersonTable();
    theTable.setShowOnlyOriginators(showOnlyOriginators);
    theTable.calculateTable();
    taskPersonTableFrame.setTable(theTable);
    taskPersonTableFrame.validate();

    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = taskPersonTableFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    taskPersonTableFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    taskPersonTableFrame.setVisible(true);
    taskPersonTableFrame.display();
  }

  public void jButtonCasePersonTable_actionPerformed(ActionEvent e) {
    if(processLog==null) {
      JOptionPane.showMessageDialog(this, "Create a process log first");
      return;
    }
/*    taskPersonTableOptionsDialog = new TaskPersonTableOptionsDialog(this, "Task-person diagram options", true);
    taskPersonTableOptionsDialog.setLocationRelativeTo(this);
    taskPersonTableOptionsDialog.validate();
    taskPersonTableOptionsDialog.setVisible(true);*/

    casePersonTableFrame = new CasePersonTableFrame();
    casePersonTableFrame.setShowOnlyMaximalArcs(showOnlyMaximalArcs);
    casePersonTableFrame.setTable(processLog.getCasePersonTable());
    casePersonTableFrame.validate();

    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = casePersonTableFrame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    casePersonTableFrame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    casePersonTableFrame.setVisible(true);
    casePersonTableFrame.display();

  }

  public void jMenuItemProcessLogTaskPersonDiagram_actionPerformed(ActionEvent
      e) {
    jButtonTaskPersonTable_actionPerformed(e);
  }

  public void jMenuItemProcessLogCasePersonDiagram_actionPerformed(ActionEvent e) {
    jButtonCasePersonTable_actionPerformed(e);
  }
}
